/*!
 * \file frdbg_report_data_format.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>
#include "frcomm.h"
#include "frmem.h"

#include "conntrack.h"
#include "frreport_comm.h"
#include "frreport_dbg.h"
#include "frct_comm.h"
#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}


int frdbg_report_data_format(Frreport_data *p_st_report_data
        , int i_flag_style
        , char **pp_data_out
        , unsigned int *p_ui_data_len);

static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Frreport_data *p_st_report_data = NULL;
    char *p_out = NULL;
    unsigned int ui_data_len = 0;
    int i_ret = 0;

    //ct
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    //p_st_body_data
    p_st_body_data = frreport_body_new_l7 (p_st_conn);

    frreport_body_l7_add_str(p_st_body_data, (char *)"key", (char *)"value");

    //p_st_report_data
    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_RESP);
    frreport_data_add_body(p_st_report_data, p_st_body_data);

    i_ret = frdbg_report_data_format(p_st_report_data, FR_YES, &p_out, &ui_data_len);

    printf("%s\n", p_out);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    frfree(p_out);
    frfree(p_st_conn);
    frreport_data_free(&p_st_report_data);
    return;

}

static void __case2(void)
{
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Frreport_data *p_st_report_data = NULL;
    char *p_out = NULL;
    unsigned int ui_data_len = 0;
    int i_ret = 0;

    //ct
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    //p_st_body_data
    p_st_body_data = frreport_body_new_l7 (p_st_conn);

    frreport_body_l7_add_int(p_st_body_data, (char *)"key1", 1);

    //p_st_report_data
    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_RESP);
    frreport_data_add_body(p_st_report_data, p_st_body_data);

    i_ret = frdbg_report_data_format(p_st_report_data, FR_YES, &p_out, &ui_data_len);

    printf("%s\n", p_out);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    frfree(p_out);
    frfree(p_st_conn);
    frreport_data_free(&p_st_report_data);

    return;

}


static void __case3(void)
{
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Frreport_data *p_st_report_data = NULL;
    char *p_out = NULL;
    unsigned int ui_data_len = 0;
    int i_ret = 0;

    //ct
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    //p_st_body_data
    p_st_body_data = frreport_body_new_l7 (p_st_conn);

    frreport_body_l7_add_double(p_st_body_data, (char *)"key2", 1.2);

    //p_st_report_data
    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_RESP);
    frreport_data_add_body(p_st_report_data, p_st_body_data);

    i_ret = frdbg_report_data_format(p_st_report_data, FR_YES, &p_out, &ui_data_len);

    printf("%s\n", p_out);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    frfree(p_out);
    frfree(p_st_conn);
    frreport_data_free(&p_st_report_data);
    return;

}

static void __case4(void)
{
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Frreport_data *p_st_report_data = NULL;
    char *p_out = NULL;
    unsigned int ui_data_len = 0;
    int i_ret = 0;

    //ct
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    //p_st_body_data
    p_st_body_data = frreport_body_new_l7 (p_st_conn);

    frreport_body_l7_add_str(p_st_body_data, (char *)"key", (char *)"value");

    //p_st_report_data
    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_RESP);
    frreport_data_add_body(p_st_report_data, p_st_body_data);

    i_ret = frdbg_report_data_format(p_st_report_data, FR_NO, &p_out, &ui_data_len);

    printf("%s\n", p_out);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    frfree(p_out);
    frfree(p_st_conn);
    frreport_data_free(&p_st_report_data);
    return;

}

static void __case5(void)
{
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Frreport_data *p_st_report_data = NULL;
    char *p_out = NULL;
    unsigned int ui_data_len = 0;
    int i_ret = 0;

    //ct
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    //p_st_body_data
    p_st_body_data = frreport_body_new_l7 (p_st_conn);

    frreport_body_l7_add_int(p_st_body_data, (char *)"key1", 1);

    //p_st_report_data
    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_RESP);
    frreport_data_add_body(p_st_report_data, p_st_body_data);

    i_ret = frdbg_report_data_format(p_st_report_data, FR_NO, &p_out, &ui_data_len);

    printf("%s\n", p_out);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    frfree(p_out);
    frfree(p_st_conn);
    frreport_data_free(&p_st_report_data);
    return;

}


static void __case6(void)
{
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Frreport_data *p_st_report_data = NULL;
    char *p_out = NULL;
    unsigned int ui_data_len = 0;
    int i_ret = 0;

    //ct
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    //p_st_body_data
    p_st_body_data = frreport_body_new_l7 (p_st_conn);

    frreport_body_l7_add_double(p_st_body_data, (char *)"key2", 1.2);

    //p_st_report_data
    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_RESP);
    frreport_data_add_body(p_st_report_data, p_st_body_data);

    i_ret = frdbg_report_data_format(p_st_report_data, FR_NO, &p_out, &ui_data_len);

    printf("%s\n", p_out);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    frfree(p_out);
    frfree(p_st_conn);
    frreport_data_free(&p_st_report_data);
    return;

}

static void __case7(void)
{
    Conntrack *p_st_conn = NULL;
    Frreport_data *p_st_report_data = NULL;
    char *p_out = NULL;
    unsigned int ui_data_len = 0;
    int i_ret = 0;

    //ct
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";


    i_ret = frdbg_report_data_format(p_st_report_data, FR_NO, &p_out, &ui_data_len);


    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    frfree(p_out);
    frfree(p_st_conn);
    frreport_data_free(&p_st_report_data);
    return;

}
int add_suite_frdbg_report_data_format(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frtds_convert frdbg_report_data_format()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);
    CU_add_test(pSuite, "case6", __case6);
    CU_add_test(pSuite, "case7", __case7);


    return FR_SUCC;
}
